const common = require('./webpack.common')
const {merge} = require('webpack-merge')
const webpack = require('webpack')
const path = require('path')
const { compilation } = require('webpack')
// 开发插件-清除bundle.js中的无用注释
class CleanComment {
    apply(complier) {
        complier.hooks.emit.tap('CleanComment', compilation => {
            // 通过compilation.assets拿到打包目录
            for (const name in compilation.assets) {
                if (name.endsWith('.js')) {
                    console.log(name);
                    const jsContent = compilation.assets[name].source()
                    const withoutComments = jsContent.replace(/\/\*+\*\//g, '') // 注意g是全局替换，否则只会替换第一处
                    compilation.assets[name] = {
                        source: () => withoutComments,
                        size: () => withoutComments.length
                    }
                }
            }
        })
    }
}
module.exports = merge(common, {
    devServer: {
        contentBase: './public',
        hot: true
    },
    output: {
        filename: 'bundle.js',
        path: path.join(__dirname, 'dist'),
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new CleanComment()
    ]
})